<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: csv_backend.php 37 2013-09-21 11:55:20Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 37 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 11:55:20 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

require('includes/application_top.php');
require(DIR_WS_CLASSES.'import.php');
require_once(DIR_FS_INC.'inc.format_filesize.php');

define('FILENAME_CSV_BACKEND', 'csv_backend.php');

switch ($_GET['action']) {

	case 'upload':
		$upload_file = $_POST['file_upload'];

		if($upload_file = &try_upload('file_upload', DIR_FS_CATALOG.'import/'))
			$upload_file_name = $upload_file->filename;
		else
			$message_stack->add(ERROR_UPLOAD_IMPORT_FILE, 'error');

		break;

	case 'import':
		$handler = new Import($_POST['select_file']);
		$mapping = $handler->map_file($handler->generate_map());
		$import = $handler->import($mapping);

		break;

	case 'export':
		if($_POST['select_content'] == 'products') {
			$handler = new Export('export.csv');
			$import = $handler->exportProdFile();

		} elseif($_POST['select_content'] == 'orders')
			$handler = new ordersExport('bestellungen_export.csv');

	break;

	case 'save':
		$configuration = $db->db_query("SELECT
											configuration_key,
											configuration_id,
											configuration_value,
											use_function,
											set_function
										FROM
											".TABLE_CONFIGURATION."
										WHERE
											configuration_group_id = '20'
										ORDER BY
											sort_order");

		while (!$configuration->EOF) {
			$db->db_query("UPDATE
								".TABLE_CONFIGURATION."
							SET
								configuration_value='".$_POST[$configuration->fields['configuration_key']]."'
							WHERE
								configuration_key='".$configuration->fields['configuration_key']."'");
			$configuration->MoveNext();
		}

		redirect(FILENAME_CSV_BACKEND);
		break;
}

$cfg_group = $db->db_query("SELECT configuration_group_title FROM ".TABLE_CONFIGURATION_GROUP." WHERE configuration_group_id = '20'");

require(DIR_WS_INCLUDES.'metatag.php'); ?>
<script type="text/javascript">
	function CSVConfig() {
		$("#config").dialog({autoOpen:false,show:'fade',height:'auto',width:500,modal:true});
		setTimeout("$('#config').dialog('open')", 250);
	}
</script>
</head>
<body>
<?php require(DIR_WS_INCLUDES.'header.php'); ?>
<div id="wrapper">
	<table class="outerTable" cellspacing="0" cellpadding="0">
		<tr>
			<td class="columnLeft2" width="<?php echo BOX_WIDTH; ?>" valign="top">
				<?php require(DIR_WS_INCLUDES.'column_left.php'); ?>
			</td>
			<td class="boxCenter" valign="top">
				<table width="100%">
					<tr>
						<td>
							<table class="table_pageHeading" width="100%">
								<tr>
									<td class="pageHeading">CSV Import / Export</td>
								</tr>
							</table>
						</td>
					</tr>
					<tr>
						<td>
							<table class="infoBoxHeading" width="100%">
								<tr>
									<td align="right">
										<button class="button" onclick="CSVConfig()" type="button"><?php echo CSV_SETUP; ?></button>
									</td>
								</tr>
							</table>
							<div id="config" class="dn">
								<?php echo draw_form('configuration', FILENAME_CSV_BACKEND, 'action=save'); ?>
									<table width="100%"  cellspacing="0" cellpadding="4">
										<?php
										$configuration = $db->db_query("SELECT
																			configuration_key,
																			configuration_id,
																			configuration_value,
																			use_function,
																			set_function
																		FROM
																			".TABLE_CONFIGURATION."
																		WHERE
																			configuration_group_id = '20'
																		ORDER BY
																			sort_order");
										$i = 0;
										while (!$configuration->EOF) { $i++;

											if (not_null($configuration->fields['use_function'])) {
												$use_function = $configuration->fields['use_function'];
												if (preg_match('/->/', $use_function)) {
													$class_method = explode('->', $use_function);
													if (!is_object(${$class_method[0]})) {
														include(DIR_WS_CLASSES.$class_method[0].'.php');
														${$class_method[0]} = new $class_method[0]();
													}
													$cfgValue = call_function($class_method[1], $configuration->fields['configuration_value'], ${$class_method[0]});

												} else
													$cfgValue = call_function($use_function, $configuration->fields['configuration_value']);

											} else
												$cfgValue = $configuration->fields['configuration_value'];

											if (((!$_GET['cID']) || (@$_GET['cID'] == $configuration->fields['configuration_id'])) && (!$cInfo) && (substr($_GET['action'], 0, 3) != 'new')) {
												$cfg_extra = $db->db_query("SELECT
																				configuration_key,
																				configuration_value,
																				date_added,
																				last_modified,
																				use_function,
																				set_function
																			FROM
																				".TABLE_CONFIGURATION."
																			WHERE
																				configuration_id = '".$configuration->fields['configuration_id']."'");

												$cInfo_array = array_merge($configuration->fields, $cfg_extra->fields);
												$cInfo = new objectInfo($cInfo_array);
											}

											if ($configuration->fields['set_function'])
												eval('$value_field = '.$configuration->fields['set_function'].'"'.htmlspecialchars($configuration->fields['configuration_value']).'");');
											else
												$value_field = draw_input_field($configuration->fields['configuration_key'], $configuration->fields['configuration_value'],'size=40');


											if (strstr($value_field, 'configuration_value'))
												$value_field=str_replace('configuration_value', $configuration->fields['configuration_key'],$value_field);

											if ($i % 2 == 0)
												$f = '';
											else
												$f = 'dataTableRow';
											echo '	<tr class="'.$f.'" >
														<td width="30%" valign="top" style="border-top: 1px dashed #ccc">
															<b>'.constant(strtoupper($configuration->fields['configuration_key'].'_TITLE')).'</b>
														</td>
														<td valign="top" style="border-top: 1px dashed #ccc">
															<table width="100%"  cellspacing="0" cellpadding="2">
																<tr>
																	<td>'.$value_field.'</td>
																</tr>
															</table>
														<br />'.constant(strtoupper( $configuration->fields['configuration_key'].'_DESC')).'
														</td>
													</tr>';

											$configuration->MoveNext();
										}
										?>
									</table>
									<?php echo '<input type="submit" class="button" onClick="this.blur();" value="'.BUTTON_SAVE.'" />'; ?>
								</form>
							</div>
							<?php
							if($import){
								if($import[0]) {
									echo '	<table width="100%">
												<tr>
													<td class="messageStackSuccess">';
														if (isset($import[0]['prod_new']))
															echo 'new products:'.$import[0]['prod_new'].'<br />';

														if (isset($import[0]['cat_new']))
															echo 'new categories:'.$import[0]['cat_new'].'<br />';

														if (isset($import[0]['prod_upd']))
															echo 'updated products:'.$import[0]['prod_upd'].'<br />';

														if (isset($import[0]['cat_upd']))
															echo 'updated categories:'.$import[0]['cat_upd'].'<br />';

														if (isset($import[0]['cat_touched']))
															echo 'touched categories:'.$import[0]['cat_touched'].'<br />';

														if (isset($import[0]['prod_exp']))
															echo 'products exported:'.$import[0]['prod_exp'].'<br />';

														if (isset($import[2]))
															echo $import[2];
									echo '			</td>
												</tr>
											</table>';
								}

								if (isset($import[1]) && $import[1][0]!='') {
									echo '	<table width="100%"  cellspacing="0" cellpadding="0">
												<tr>
													<td class="messageStackError">';
														for ($i=0;$i<count($import[1]);$i++)
															echo $import[1][$i].'<br />';
									echo '			</td>
												</tr>
											</table>';
								}
							}
							?>
							<table width="100%"  cellspacing="2" cellpadding="5">
								<tr>
									<td class="pageHeading">Import</td>
								</tr>
								<tr>
									<td class="dataTableHeadingContent"><?php echo TEXT_IMPORT; ?>
										<table width="100%"  cellspacing="2" cellpadding="0">
											<tr>
												<td width="7%"></td>
												<td width="93%" class="infoBoxHeading"><?php echo UPLOAD; ?></td>
											</tr>
											<tr>
												<td>&nbsp;</td>
												<td>
													<?php
														echo draw_form('upload',FILENAME_CSV_BACKEND,'action=upload','POST','enctype="multipart/form-data"');
														echo draw_file_field('file_upload');
														echo '<br/><button type="submit" class="button" onclick="this.blur();"><span class="button_upload">'.BUTTON_UPLOAD.'</span></button>';
														echo '</form>';
													?>
												</td>
											</tr>
											<tr>
												<td><br /><br /></td>
												<td class="infoBoxHeading"><?php echo SELECT; ?></td>
											</tr>
											<tr>
												<td>&nbsp;</td>
												<td>
													<?php
														$files=array();
														echo draw_form('import',FILENAME_CSV_BACKEND,'action=import','POST','enctype="multipart/form-data"');
														if ($dir= opendir(DIR_FS_CATALOG.'import/')){
															while (($file = readdir($dir)) !==false) {
																if (is_file(DIR_FS_CATALOG.'import/'.$file) && ($file !=".htaccess") && ($file !="index.html")){
																	$size=filesize(DIR_FS_CATALOG.'import/'.$file);
																	$files[]=array('id' => $file, 'text' => $file.' | '.format_filesize($size));
																}
															}
															closedir($dir);
														}
														echo draw_pull_down_menu('select_file',$files,'');
														echo '<br/><input type="submit" class="button" onclick="this.blur();" value="'.BUTTON_IMPORT.'"/>';
														echo '</form>';
													?>
												</td>
											</tr>
										</table>
										<p>&nbsp; </p>
									</td>
								</tr>
							</table>

							<table width="100%"  cellspacing="5" cellpadding="0">
								<tr>
									<td class="pageHeading">Export</td>
								</tr>
								<tr>
									<td class="infoBoxHeading">
										<table width="100%" cellspacing="2" cellpadding="0">
											<tr>
												<td width="7%">&nbsp;</td>
												<td width="93%">
												<?php
													echo TEXT_EXPORT;
													echo draw_form('export',FILENAME_CSV_BACKEND,'action=export','POST','enctype="multipart/form-data"');
													$hersteller = $db->db_query("SELECT
																					brand_id,
																					brand_name
																				FROM
																					".TABLE_BRAND);
													$man_content = array();

													$man_content[] = array('id' => '0', 'text' => 'Alle Hersteller');

													while(!$hersteller->EOF) {
														$man_content[] = array('id' => $hersteller->fields['brand_id'], 'text' => $hersteller->fields['brand_name']);
														$hersteller->MoveNext();
													}

													echo draw_pull_down_menu('select_man',$man_content,'brand');
													echo draw_pull_down_menu('select_cat', get_category_tree(), 'category');
													$content = array();
													$content[] = array('id'=>'products','text' => TEXT_PRODUCTS);
													$content[] = array('id'=>'orders','text' => 'Bestellungen');
													echo draw_pull_down_menu('select_content', $content, 'products');
													echo '<br/><input type="submit" class="button" onclick="this.blur();" value="'.BUTTON_EXPORT.'"/>';
													echo '</form>';
												?>
												</td>
											</tr>
										</table>
									</td>
								</tr>
							</table>
						</td>
					</tr>
				</table>
			</td>
		</tr>
	</table>
</div>
<?php require(DIR_WS_INCLUDES.'footer.php'); ?>
</body>
</html>
<?php require(DIR_WS_INCLUDES.'application_bottom.php'); ?>